package com.jml.防盗链;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ImageFilter implements Filter {
	public void init(FilterConfig filterConfig) {
		System.out.println("初始化...");
	}
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		System.out.println("doFilter....");
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse res = (HttpServletResponse) response;
		//获取请求头中来源
		String referer = req.getHeader("referer");
		//获取当前请求名称
		String serverName = request.getServerName();
		System.out.println("referer:"+referer+"----serverName:"+serverName+":"+serverName);
		if(referer==null||(!referer.contains(serverName))){
			req.getRequestDispatcher("/imgs/error.png").forward(req, res);
			return ;
		}
		chain.doFilter(req, res);
	}
	public void destroy() {

	}
}

